package dao;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.sql.*;
import java.util.*;

public class basedb {
    String driver="com.mysql.jdbc.Driver";
    String url="jdbc:mysql://127.0.0.1:3306/ddd";
    String username="root";
    String password="123";
    Connection conn;
    PreparedStatement ps;
    ResultSet rs;

    public static void main(String[] args){
        System.out.print("ss");
        basedb db=new basedb();
        db.getConnection();

    }
    public void getConnection(){
        try {
            //加载mysql驱动类
            Class.forName(driver);

            conn =DriverManager.getConnection(url,username,password);
            System.out.print("数据库运行成功");

        }catch(Exception e){
            e.printStackTrace();
            System.out.print("数据库运行失败");
        }
    }


    public List selectUser(String sql) {
        //获取数据库连接
        getConnection();
        //定义一个list集合
        List userList = new ArrayList();
        try {
            ps=conn.prepareStatement(sql);
            //执行数据库查找
            rs = ps.executeQuery();
            while (rs.next()) {
                ResultSetMetaData mdata = rs.getMetaData();
                int count = mdata.getColumnCount();// 列的数量
                //定一个HashMap作为你的行数据集合
                Map<String, Object> map = new HashMap<String, Object>();
                //循环获取每行每列的值
                for (int i = 1; i <= count; i++) {
                    String label = mdata.getColumnLabel(i);
                    Object obj = rs.getObject(i);
                    map.put(label.toLowerCase(), obj);
                }
                userList.add(map);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return userList;
    }




    //查询数据库的数据
    public List select(String username,String password){
        //定义一个json对象
        JSONObject obj=new JSONObject();

        //定义一个json数组
        JSONArray list=new JSONArray();

        getConnection();
        String sql="select * from userbiao where user_name='"+username+"'and user_password='"+ password+"'";
        try{
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            if(rs.next()){
                rs.previous();//将结果向上遍历一次
                while (rs.next()){
                    System.out.print("sjk"+rs.getInt(1));
                    //往json对象里面添加值
                    obj.put("code","1");
                    obj.put("username",rs.getString(2));
                    obj.put("password",rs.getString(3));
                    System.out.print("sjk"+rs.getString(1));
                    //往json数组里面添对象
                    list.add(obj);
                }
            }else{
                obj.put("code","0");
                list.add(obj);
            }

            rs.close();
            ps.close();

        }catch(Exception e){
            e.printStackTrace();
        }
        return list;
    }

    //向数据库添加数据
    public String add(String username,String password){
        String rss="{\"result\": \" 0\"}";
        getConnection();
        String sql="insert into userbiao(user_name,user_password)value('"+username+"','"+ password+"')";
        try{
            ps=conn.prepareStatement(sql);
            rss="{\"result\": \" "+ps.executeUpdate()+"\"}";
            ps.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return rss;
    }
}
